class GridBot(breve.Mobile):
	def __init__(self, unit):
		breve.Mobile.__init__(self)
		self.unit = unit
		self.setShape(breve.createInstances(breve.Cube, 1).initWith(
			breve.vector(unit, unit, unit)))
		self.setColor(breve.vector(1, 0, 0))
		self.handleCollisions('GridBot', 'botCollision')
	
	def iterate(self):
		breve.Mobile.iterate(self)
		self.neighborList = self.getNeighbors()
		self.look("Right", "Center")	
		
	def botCollision(self):
		self.setColor(breve.vector(
			breve.randomExpression(1),
			breve.randomExpression(1),
			breve.randomExpression(1)))
		
	def look(self, xDirection, zDirection):
		x = 0
		z = 0
	
		if xDirection != "Center":
			if xDirection == "Left": x -= (self.unit - 1)
			else: x += (self.unit - 1)

		if zDirection != "Center":
			if zDirection == "Up": z -= (self.unit - 1)
			else: z += (self.unit - 1)
		
		while len(self.neighborList) > 0:
			neighbor = self.neighborList.pop()
			if (neighbor.getLocation()[0] > self.getLocation()[0] - self.unit / 2 + x and
			    neighbor.getLocation()[0] < self.getLocation()[0] - self.unit / 2 + self.unit + x and
			    neighbor.getLocation()[2] < self.getLocation()[2] + self.unit / 2 + z and
				neighbor.getLocation()[2] > self.getLocation()[2] + self.unit / 2 - self.unit + z):
					neighbor.setColor(breve.randomExpression(breve.vector(1, 1, 1)))
			
	def walk(self, xDirection, zDirection):
		x = 0
		z = 0
		
		if xDirection != "Center":
			if xDirection == "Left": x -= self.unit
			else: x += self.unit	
	
		if zDirection != "Center":
			if zDirection == "Up": z -= self.unit
			else: z += self.unit
	
		self.move(breve.vector(
			self.getLocation()[0] + x, 
			self.getLocation()[1], 
			self.getLocation()[2] + z))

class ExitBot(GridBot):
	def __init__(self, unit, exit):
		GridBot.__init__(self, unit)
		self.exit = exit
		self.getOut = False	
	
	def iterate(self):
		GridBot.iterate(self)
		exitDirection = self.getExitDirection()
		print exitDirection
		print self.getLocation()
		print self.exit
		self.walk(exitDirection[0], exitDirection[1])	
			
	def getExitDirection(self):
		x = "Center"
		z = "Center"
		if self.getLocation()[0] < self.exit[0]: x = "Right"
		if self.getLocation()[0] > self.exit[0]: x = "Left"
		if self.getLocation()[2] < self.exit[2]: z = "Down"
		if self.getLocation()[2] > self.exit[2]: z = "Up"
		return [x, z]